package com.ylbh.modules.businessorder.controller;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.businessorder.dto.BusinessOrderDTO;
import com.ylbh.common.pojo.businessorder.vo.BusinessOrderDetailsVO;
import com.ylbh.common.pojo.businessorder.vo.NewBusinessOrderDetailsVO;
import com.ylbh.common.pojo.businessorder.vo.SubOrderDetailsVO;
import com.ylbh.common.utils.CommonResult;
import com.ylbh.common.utils.Ref;
import com.ylbh.modules.businessorder.service.ShoppingBusinessOrderService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ylbh.common.utils.PageUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 *
 * 联盟店订单
 * @author czx
 * @date 2019-04-17 14:10:36
 */
@RestController
@RequestMapping("business/order")
public class ShoppingBusinessOrderController {

    @Autowired
    private ShoppingBusinessOrderService shoppingBusinessOrderService;

    @ApiOperation("联盟店详情")
    @RequestMapping("getOrderByIdOrPayNo")
    @RequiresPermissions("order:shoppingBusinessOrder:getOrderByIdOrPayNo")
    public CommonResult<BusinessOrderDetailsVO> getOrderByIdOrPayNo(Long id, String payNo, String orderType){
        BusinessOrderDetailsVO byId = shoppingBusinessOrderService.getOrderByIdOrPayNo(id, payNo, orderType);
        return Ref.ok(byId);
    }

    @ApiOperation("联盟店详情-新版")
    @RequestMapping("getNewOrderByIdOrPayNo")
    @RequiresPermissions("order:shoppingBusinessOrder:getOrderByIdOrPayNo")
    public CommonResult getNewOrderByIdOrPayNo(Long id, String payNo, String orderType){
        NewBusinessOrderDetailsVO byId = shoppingBusinessOrderService.getNewOrderByIdOrPayNo(id, payNo, orderType);
        return Ref.ok(byId);
    }

    @ApiOperation("树形联盟店订单列表")
    @RequestMapping("queryOrderTreeList")
    @RequiresPermissions("order:shoppingBusinessOrder:queryOrderTreeList")
    public CommonResult<PageUtils> queryOrderTreeList(BusinessOrderDTO businessOrderListDTO){
        PageUtils list = shoppingBusinessOrderService.queryOrderTreeList(businessOrderListDTO);
        return Ref.ok(list);
    }

    @ApiOperation("子订单详情")
    @RequestMapping("/getOrderByPayNO")
    @RequiresPermissions("order:shoppingBusinessOrder:getOrderByPayNO")
    public CommonResult<SubOrderDetailsVO> getOrderByPayNO(String id1){
        SubOrderDetailsVO orderByPayNO = shoppingBusinessOrderService.getOrderByPayNO(id1);
        return Ref.ok(orderByPayNO);
    }

    @ApiOperation("导出联盟店订单")
    @RequestMapping("businessOrderExcel")
    @RequiresPermissions("order:shoppingBusinessOrder:businessOrderExcel")
    public CommonResult<Void> businessOrderExcel(HttpServletResponse response, HttpServletRequest request) throws NormalException {
        shoppingBusinessOrderService.businessOrderExcel(response,request);
        return Ref.ok();
    }

    @SysLog("批量修改联盟店订单状态")
    @ApiOperation("批量修改联盟店订单状态")
    @RequestMapping("modifyBatchOrderStatusById/{shopId}")
    @RequiresPermissions("order:shoppingBusinessOrder:modifyBatchOrderStatusById")
    public CommonResult modifyBatchOrderStatusById(@RequestBody String[] payNos, @PathVariable String[] shopId) {
        if (payNos.length > 0 && shopId.length > 0){
            String str = "";
            List<String> payNO = Arrays.asList(payNos);
            List<String> shop = Arrays.asList(shopId);
            for (int i = 0; i < payNO.size(); i++) {
                str = str + "/" +payNO.get(i) + "," + shop.get(i);
            }
            if (StringUtils.isNotBlank(str)){
                int length = str.length();
                String payNo = str.substring(1, length);
                String s = shoppingBusinessOrderService.modifyBatchOrderStatusById(payNo);
                return Ref.ok(s);
            }else {
                return Ref.error("信息有误");
            }
        }
        return Ref.ok();

    }

}
